package servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.w3c.dom.Node;

import domain.Movie;

import entity.User;
import entity.Watched;

import service.WatchedFilmService;
import util.WebUtil;
import util.XmlUtil;

/**
 * Servlet implementation class MyWatchedFilms
 */
@WebServlet(urlPatterns = { "/watched" })
public class MyWatchedFilms extends HttpServlet {

	WatchedFilmService wservice = new WatchedFilmService();

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		if (session.getAttribute("user") != null) {
			User user = (User) session.getAttribute("user");
			ArrayList<Watched> watched = wservice.getWatchedFilmsByUser(user);
			String domainName=WebUtil.getURLWithContextPath(request);
			try {
				ArrayList<Movie> movies=XmlUtil.getWatchedMovies(watched, domainName);
				request.setAttribute("movies", movies);
				RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/watched.jsp");
				if (dispatcher != null)	dispatcher.forward(request, response);
			} catch (Exception e) {
				request.setAttribute("error", e.getMessage());
				RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/watched.jsp");
				if (dispatcher != null)	dispatcher.forward(request, response);
			}
		} else {
			response.sendRedirect("");
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
}
